const query = require('./fun')
const sqlCount = 'SQL_CALC_FOUND_ROWS';
const { ADMIN, USER, NEWS, TYPE, SWIPER, SENSITIVES } = require('./database');
const { sqlAddStr, sqlWhereStr, sqlSelectStr, sqlUpdateStr } = require('../util/comm/comm')
/**
 * 判断是否存在注册用户
 */
function isAdmin(data) {
    return new Promise((resolve, reject) => {
        query(`select  adminId,adminName from ${ADMIN} where adminName=? `, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 添加管理员
 */
function adminLogin(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${ADMIN}(${obj.keys}) VALUES (${obj.vals})`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 根据id获取用户信息
 */
function userById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${USER} where userId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 根据id获取管理员信息
 */
function adminById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${ADMIN} where adminId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取用户列表
 */
function getUserList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} * from ${USER} ${condition}  limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改用户状态
 */
function setUserState(id, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${USER} SET ${sql} WHERE userId=${id}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 添加用户
 */
function userSave(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${USER}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改用户信息
 */
function userUpdate(id, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${USER} SET ${sql} WHERE userId=${id}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 删除用户
 */
function userDelete(userId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${USER} WHERE userId=?`, { userId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除多个用户
 */
function userAllDelete(userId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${USER} WHERE userId in (${userId.toString()})`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取新闻分类列表
 */
function getTypeList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} * from ${TYPE} ${condition}  limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加分类
 */
function typeSave(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${TYPE}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改分类信息
 */
function typeUpdate(id, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${TYPE} SET ${sql} WHERE typeId=${id}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 删除分类
 */
function typeDelete(typeId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${TYPE} WHERE typeId=?`, { typeId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取全部分类
 */
function typeListAll() {
    return new Promise((resolve, reject) => {
        query(`select ${sqlCount} * from ${TYPE}`, {}, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取新闻信息列表
 */
function newsList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} ${NEWS}.*from ${NEWS} ${condition}  limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改新闻审核状态
 */
function setnewsState(id, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${NEWS} SET ${sql} WHERE newsId=${id}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 添加新闻
 */
function newsSave(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${NEWS}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除新闻
 */
function newsDelete(newsId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${NEWS} WHERE newsId=?`, { newsId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取轮播图
 */
function swiperList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} * from ${SWIPER} ${condition}  limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加轮播图
 */
function swiperSave(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${SWIPER}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改轮播图
 */
function swiperUpdate(id, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${SWIPER} SET ${sql} WHERE swiperId=${id}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 删除轮播图
 */
function swiperDelete(swiperId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${SWIPER} WHERE swiperId=?`, { swiperId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取所有的tag标签
 */
function sensitivesListAll() {
    return new Promise((resolve, reject) => {
        query(`select ${sqlCount} * from ${SENSITIVES}`, {}, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加敏感词
 */
function sensitivesSave(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${SENSITIVES}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除敏感词
 */
function sensitivesDelete(sensitiveId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${SENSITIVES} WHERE sensitiveId=?`, { sensitiveId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取管理员
 */
function adminList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} * from ${ADMIN} ${condition}  limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加管理员
 */
function adminSave(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${ADMIN}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除管理员
 */
function adminDelete(adminId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${ADMIN} WHERE adminId=?`, { adminId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取用户的数目
 */
function userCount() {
    return new Promise((resolve, reject) => {
        query(`select count(userId) as counts from ${USER}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取新闻的数目
 */
function newsCount() {
    return new Promise((resolve, reject) => {
        query(`select count(newsId) as counts from ${NEWS}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取新增的用户数目
 */
function newsCountToday() {
    return new Promise((resolve, reject) => {
        query(`select count(newsId) as counts from ${NEWS} where TO_DAYS(newsTime) = TO_DAYS(NOW())`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取一周用户数据
 */
function userWord() {
    return new Promise((resolve, reject) => {
        query(`select  ${sqlCount} * from ${USER} where YEARWEEK(date_format(userTime,'%Y-%m-%d'),1) = YEARWEEK(now(),1)`, {}, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 获取一周用户数据
 */
function newsWord() {
    return new Promise((resolve, reject) => {
        query(`select  ${sqlCount} * from ${NEWS} where YEARWEEK(date_format(newsTime,'%Y-%m-%d'),1) = YEARWEEK(now(),1)`, {}, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 获取不同分类新闻数目
 */
function newtypeNum(typeList) {
    let sql = []
    sql = typeList.map(newsType => {
        return `select count(newsId)  value,'${newsType}'  name from ${NEWS} where newsType='${newsType}'`
    })
    return new Promise((resolve, reject) => {
        query(sql)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
exports.isAdmin = isAdmin;
exports.adminLogin = adminLogin;
exports.getUserList = getUserList;
exports.userById = userById;
exports.adminById = adminById;
exports.setUserState = setUserState;
exports.userSave = userSave;
exports.userUpdate = userUpdate;
exports.userDelete = userDelete;
exports.userAllDelete = userAllDelete;
exports.getTypeList = getTypeList;
exports.typeSave = typeSave;
exports.typeUpdate = typeUpdate;
exports.typeDelete = typeDelete;
exports.typeListAll = typeListAll;
exports.newsList = newsList;
exports.setnewsState = setnewsState;
exports.newsSave = newsSave;
exports.newsDelete = newsDelete;
exports.swiperList = swiperList;
exports.swiperSave = swiperSave;
exports.swiperUpdate = swiperUpdate;
exports.swiperDelete = swiperDelete;
exports.sensitivesListAll = sensitivesListAll;
exports.sensitivesSave = sensitivesSave;
exports.sensitivesDelete = sensitivesDelete;
exports.adminList = adminList;
exports.adminSave = adminSave;
exports.adminDelete = adminDelete;
exports.userCount = userCount;
exports.newsCount = newsCount;
exports.newsCountToday = newsCountToday;
exports.userWord = userWord;
exports.newsWord = newsWord;
exports.newtypeNum = newtypeNum;